<?php
namespace app\mp\admin;

use app\system\admin\Admin;
use app\common\service\Order as OrderService;
use app\common\service\Material as MaterialService;
use app\common\service\DistributingFeed as DistributingFeedService;

/**
 * 补料任务
 * @author King
 *
 */
class DistributingFeed extends Admin
{

	protected $hisiModel = 'DistributingFeed';//模型名称[通用添加、修改专用]
	protected $hisiValidate = 'DistributingFeed';
	protected $hisiAddScene = '';//添加数据验证场景名
	protected $hisiEditScene = '';//更新数据验证场景名

    public function initialize() {
    	parent::initialize();
    }

    public function index()
    {
    	if ($this->request->isAjax()) {
    		$where      = $data = [];
    		$page       = $this->request->param('page/d', 1);
    		$limit      = $this->request->param('limit/d', 20);
    		$clientOrdernum    = $this->request->param('client_ordernum/s');
    		$materialName    = $this->request->param('material_name/s');
    		$materialSku    = $this->request->param('material_sku/s');

    		if ($clientOrdernum != '') {
    			$where[] = ['client_ordernum', '=', $clientOrdernum];
    		}
            switch (env('env_site')) {
                case 'riwei':
                    $ext_id = trim($this->request->param('ext_id'));
                    if ($ext_id != '') {
                        $temp = db('order')->field('id')->where([['ext_id', '=', $ext_id]])->find();
                        $where[] = ['order_id', '=', $temp['id']];
                    }
                    break;
            }
    		if ($materialName != '') {
    			$where[] = ['material_id', 'in', db('material')->where('name', 'like', "%{$materialName}%")->column('id')];
    		}
    		if ($materialSku != '') {
    			$where[] = ['material_id', 'in', db('material')->where('material_sku', '=', $materialSku)->column('id')];
    		}

    		$order = 'status asc,id desc';
    		$data['data'] = db('distributing_feed')->where($where)->page($page)->limit($limit)->order($order)->select();
    		$materialService = new MaterialService();
    		foreach ($data['data'] as $k => $v) {
    			$data['data'][$k]['mInfo'] = $materialService->getInfoById($v['material_id'], 'name,material_sku,color,colornum,model,supplier');
    		}

    		$data['count'] = db('distributing_feed')->where($where)->count('id');
    		$data['code'] = 0;
    		$data['msg'] = '';
    		return json($data);
    	}

    	$assign = [];
    	return $this->assign($assign)->fetch();
    }

    /**
     * 开始采购
     *
     *
     * @author KING
     * @date 2020年6月3日
     *
     */
    public function startPurchasing() {
    	$materialId = $this->request->param('material_id/d', 0);
    	$orderId = $this->request->param('order_id/d', 0);

    	$orderService = new OrderService();
    	$distributingFeedService = new DistributingFeedService();
    	$result = $distributingFeedService->useStock($materialId, $orderId, true);
    	$orderInfo = $orderService->getInfoById($orderId);
    	$materialName = getMaterialName($materialId);
    	if ($result['ret'] == 1) {
    		db('order_material')->where(['order_id' => $orderId, 'material_id' => $materialId])->update(['is_caigou_start' => 2 ,'caigou_finish_time' => time(),]);
    		$this->addOrderLog($orderId, '', "使用库存完成物料[{$materialName}]的采购");
    	} elseif ($result['ret'] == 2) {
    		db('order_material')->where(['order_id' => $orderId, 'material_id' => $materialId])->update(['is_caigou_start' => 1 ,'caigou_start_time' => time()]);
    		$this->addOrderLog($orderId, '', "物料[{$materialName}]开始采购并生成补料任务单（feed_id：{$result['data']['feed_id']}）");
    	}

    	return $this->Json($result['ret'], $result['msg'], $result['data']);
    }

}